package com.wxgo.controller.admin;

import java.util.List;

import com.jfinal.core.Controller;
import com.wxgo.common.model.Role;
import com.wxgo.common.model.User;
import com.wxgo.factory.DaoFactory;

public class UserController extends Controller {

	public void manage() {
		List<User> userList = DaoFactory.userDao().find("select * from user");
		setAttr("tableList", userList);
		render("/admin/user/userManage.html");
	}

	public void add() {
		List<Role> roleList = DaoFactory.roleDao().findAll();
		setAttr("roleList", roleList);
		render("/admin/user/userAdd.html");
	}

	public void save() {
		User user = getModel(User.class, "user");
		user.save();
		manage();
		return;
	}

	public void delete() {
		String id = getPara("id");
		if ("1".equals(id)) {
			manage();
			return;
		}
		DaoFactory.userDao().delete("user", id);
		manage();
	}

	public void edit() {
		Integer id = getParaToInt("id");
		User user = DaoFactory.userDao().findById(id);
		setAttr("user", user);
		List<Role> roleList = DaoFactory.roleDao().findAll();
		setAttr("roleList", roleList);
		render("/admin/user/userEdit.html");
	}

	public void update() {
		User user = getModel(User.class, "user");
		user.update();
		manage();
		return;
	}

}
